import Vue from 'vue'
import Router from 'vue-router'
import MyLogin from './pages/MyLogin.vue'
import MyHome from './pages/MyHome.vue'
import MyWelcome from './pages/MyWelcome.vue'

import MyUsers from './components/user/MyUsers.vue'

import MyRights from './components/power/MyRights.vue'
import MyRoles from './components/power/MyRoles.vue'

import MyGoods from './components/goods/MyGoods.vue'
import MyParams from './components/goods/MyParams.vue'
import MyCategories from './components/goods/MyCategories.vue'

import MyOrders from './components/order/MyOrders.vue'
import MyReports from './components/user/MyReports.vue'

import AddGoods from './components/goods/AddGoods.vue'
Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: '/mylogin'},
        { path: '/mylogin', component: MyLogin},
        { 
            path: '/myhome', 
            component: MyHome,
            redirect: 'MyWelcome',
            children: [
                {path: '/mywelcome', component: MyWelcome},
                
                {path: '/Users', component: MyUsers},
                {path: '/rights', component: MyRights},
                {path: '/roles', component: MyRoles},
                {path: '/goods', component: MyGoods},
                {path: '/params', component: MyParams},
                {path: '/categories', component: MyCategories},
                {path: '/orders', component: MyOrders},
                {path: '/reports', component: MyReports},
                {path: '/goods/AddGoods', component: AddGoods}
            ]
        },
    ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪个路径跳转过来
    //next 强制跳转
    if (to.path === '/MyLogin') return next()
    //获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/MyLogin')
    next()
})

export default router